import time
from .data import load_resolveAlertdataAll,load_resolveAlertdataOne,baseurl
import allure
import pytest
import requests
import os
basedir=os.path.dirname(__file__)
filepath1=os.path.join(basedir,"testCase/resolveAlertAll.json")
filepath2=os.path.join(basedir,"testCase/resolveAlertOne.json")
@allure.epic("处理报警接口")
class TestResolveAlert:
    def setup_class(self):
        self.session=requests.session()
        self.url1=f"{baseurl}/login"
        self.url2 = f"{baseurl}/alerts/resolve_all"
        self.url3=f"{baseurl}/alerts/resolve_one"
        print("处理报警接口测试开始：", time.strftime("%Y-%m-%d-%H:%M:%S"))
    def teardown_class(self):
        self.session.close()
        print("处理报警接口测试结束：", time.strftime("%Y-%m-%d-%H:%M:%S"))

    @allure.story("处理用户全部的报警记录")
    @pytest.mark.parametrize("testCase,login_name,login_password,target_username,msg,status_code",load_resolveAlertdataAll(filepath1))
    @allure.title("{testCase}")
    @pytest.mark.order(2)
    def test_resolveAll(self,testCase,login_name,login_password,target_username,msg,status_code):
        self.session.post(self.url1,data={"username":login_name,"password":login_password})
        responses=self.session.post(f"{self.url2}/{target_username}")
        assert status_code==responses.status_code
        assert msg in responses.text
        self.session.cookies.clear()

    @allure.story("处理单条报警记录")
    @pytest.mark.parametrize("testCase,login_name,login_password,alert_id,msg,status_code",load_resolveAlertdataOne(filepath2))
    @allure.title("{testCase}")
    @pytest.mark.order(1)
    def test_resolveAletOne(self,testCase,login_name,login_password,alert_id,msg,status_code):
        self.session.post(self.url1,data={"username":login_name,"password":login_password})
        responses=self.session.post(f"{self.url3}/{alert_id}")
        assert status_code==responses.status_code
        assert msg in responses.text
        self.session.cookies.clear()
